package com.tianheng.wanzhou.datacollecting.netty.zhengjing.receiver.receiverImpl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tianheng.wanzhou.datacollecting.DataCollectingApplication;
import com.tianheng.wanzhou.datacollecting.db.entity.KaiyuanZhengjing;
import com.tianheng.wanzhou.datacollecting.db.entity.KaiyuanZhengjingHis;
import com.tianheng.wanzhou.datacollecting.db.service.IKaiyuanZhengjingHisService;
import com.tianheng.wanzhou.datacollecting.db.service.IKaiyuanZhengjingService;
import com.tianheng.wanzhou.datacollecting.netty.zhengjing.receiver.Receiver;
import com.tianheng.wanzhou.datacollecting.util.Data;
import com.tianheng.wanzhou.datacollecting.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yujw
 * @Date: 2020/5/19 13:11
 * @Version 1.0
 */
@Component
public class KaiyReceiver extends Receiver {

    @Autowired
    IKaiyuanZhengjingService service;

    @Autowired
    IKaiyuanZhengjingHisService hisService;

    @Override
    public Object parseMessage(String message, String ip, Integer company, Integer workshop, String machineId) {
        Map<String, Data> map = new HashMap<>();
        map.put("machineIp", new Data(ip));
        map.put("company", new Data(company));
        map.put("workshop", new Data(workshop));
        map.put("machineId", new Data(machineId));

        Class kaiy = KaiyuanZhengjing.class;
        Field[] fields = kaiy.getDeclaredFields();
        int i = 0;
        for (Field field : fields) {
            String fieldName = field.getName();
            if (fieldName.equals("machineIp") || fieldName.equals("company") ||
                    fieldName.equals("workshop") || fieldName.equals("machineId")){
                continue;
            }
//            if (field.getName().equals("machineState")){
//                map.put(field.getName(), new Data(new String(StrUtil.hexStr2Byte(message.substring(4 * i, 4 * (i + 1))))));
//                continue;
//            }
            if (i > 0 && i < 7){
                map.put(field.getName(), new Data(StrUtil.hexStr2Int(message.substring(4 * i, 4 * (i + 2)))));
                i += 2;
            }else {
                if (fieldName.equals("allLength") || fieldName.equals("jiaYield") || fieldName.equals("yiYield") ||
                        fieldName.equals("bingYield") || fieldName.equals("dingYield")){
                    int length = StrUtil.hexStr2Int(message.substring(4 * i, 4 * (i + 1)));
                    map.put(field.getName(), new Data(length *1000));
                }else {
                    map.put(field.getName(), new Data(StrUtil.hexStr2Int(message.substring(4 * i, 4 * (i + 1)))));
                }
                i++;
            }
        }
        KaiyuanZhengjing kaiyuanZhengjing = (KaiyuanZhengjing) Data.bind(new KaiyuanZhengjing(), map);
        KaiyuanZhengjingHis lastRecord = DataCollectingApplication.kaiyuanZhengjingHis.get(ip);
        KaiyuanZhengjingHis kaiyuanZhengjingHis = (KaiyuanZhengjingHis) Data.bind(new KaiyuanZhengjingHis(), map);
        double now = kaiyuanZhengjing.getActualLength();
        double last = 0.00;
        if (null != lastRecord) {
            last = lastRecord.getActualLength();
//            if (lastRecord.getDuanshaErr() == 1 && huafangZhengjing.getDuanshaErr() == 0) {
//                huafangZhengjing.setStopNum(DataCollectingApplication.stopNum.get(ip) + 1);
//                DataCollectingApplication.stopNum.put(ip, DataCollectingApplication.stopNum.get(ip) + 1);
//
//            }
        }
        if (now < last){
            hisService.save(lastRecord);
            DataCollectingApplication.kaiyuanZhengjingHis.put(ip, kaiyuanZhengjingHis);
        } else {
            DataCollectingApplication.kaiyuanZhengjingHis.put(ip, kaiyuanZhengjingHis);
        }
        //存环思erp



        return kaiyuanZhengjing;
    }

    @Override
    public IService getService() {
        return service;
    }
}
